package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    public void work(BookList bookList){
        System.out.println("新增图书....");
        //判满
        int size = bookList.getBooksize();
        if(size == bookList.getBooks().length){
            System.out.println("书架满了！不能放了！");
            return;
        }
        //输入新增图书
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要新增图书的名字：");
        String name = scanner.nextLine();
        System.out.println("请输入你要新增图书作者的名字：");
        String author = scanner.nextLine();
        System.out.println("请输入你要新增图书的类型：");
        String type = scanner.nextLine();
        System.out.println("请输入你要新增图书的价钱：");
        int  price = scanner.nextInt();
        //查重
        for (int i = 0; i < size; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)){
                System.out.println("有这本书了！");
                System.out.println(book);
                return;
            }
        }
        //增加
        Book newbook = new Book(name,author,price,type);
        bookList.setBook(size,newbook);
        //有效个数加1
        bookList.setBooksize(size+1);
        System.out.println("新增成功！！");
    }
}
